var kv220=[];
var kv500=[];
$(function(){
	var tid = $('#tid').val();
	getData(tid);
});
//根据台风id获取相关故障
function getData(tid) {
	if(tid){
		$.getJSON('/rest/TyphoonRestService/getFaultByTyphoonId/' + tid+"/all",
				function(result) {
			getChartDatas(result);
		});
	}
}
function getChartDatas(faults){
	for(var i in faults){
		if(faults[i].faultRecord.voltage==220){
			kv220.push(faults[i]);
		}else{
			kv500.push(faults[i]);
		}
	}
	drawChart();
	showTyphoonFault();
	showFaultTb();
}
function showTyphoonFault() {
	$('#typhoonTitle').html(
			 "本次台风共发生故障<span class='count'>"
			+ (kv500.length+kv220.length) + "</span>次，其中500kV<span class='count'>"+kv500.length+"</span>次，220kV<span class='count'>"+kv220.length+"</span>次。");
	
}
var pie = new Pie("pie");
function drawChart(){
	pie.setPie([['500kV',kv500.length],['220kV',kv220.length]]);
	var option = pie.getOption();
	option.series[0].label.normal.formatter="{b}:{c}({d}%)";
	pie.show();
}
function showFaultTb(){
	var map500={};
	for(var i in kv500){
		var faulteQuipName=kv500[i].faultRecord.equipment;
		var faultCount=map500[faulteQuipName];
		if(faultCount){
			faultCount++;
		}else{
			faultCount=1;
		}
		map500[faulteQuipName]=faultCount;
	}
	var arr500=[];
	for(var ename in map500){
		arr500.push([ename,map500[ename]]);
	}
	var map220={};
	for(var i in kv220){
		var faulteQuipName=kv220[i].faultRecord.equipment;
		var faultCount=map220[faulteQuipName];
		if(faultCount){
			faultCount++;
		}else{
			faultCount=1;
		}
		map220[faulteQuipName]=faultCount;
	}
	var arr220=[];
	for(var ename in map220){
		arr220.push([ename,map220[ename]]);
	}
	arr500.sort(function(x,y){
		return y[1]-x[1];
	});
	arr220.sort(function(x,y){
		return y[1]-x[1];
	});
	init500Tb(arr500);
	init220Tb(arr220);
}
var tb500;
var tb220;
function init500Tb(arr500){
	var opt = MyTable.getDefaultOptions();
	opt.paging = false;
	opt.scrollY = '380px';
	var columns = [ '故障设备', '故障次数'];
	tb500 = new MyTable('fault500Tb', columns, opt);
	var data=[];
	for(var i=0;i<arr500.length;i++){
		data.push(arr500[i]);
		if(i==9){
			break;
		}
	}
	tb500.setDataSet(data);
	tb500.show();
}
function init220Tb(arr220){
	var opt = MyTable.getDefaultOptions();
	opt.paging = false;
	opt.scrollY = '380px';
	var columns = [ '故障设备', '故障次数'];
	tb220 = new MyTable('fault220Tb', columns, opt);
	var data=[];
	for(var i=0;i<arr220.length;i++){
		data.push(arr220[i]);
		if(i==9){
			break;
		}
	}
	tb220.setDataSet(data);
	tb220.show();
}
function resizeHtml() {
	if (pie) {
		pie.getChart().resize();
	}
	if (tb500) {
		tb500.getTable().draw();
	}
	if (tb220) {
		tb220.getTable().draw();
	}
}